/**
 * 统一的消息提示工具
 */
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'

// 成功消息
export const showSuccess = (message: string, duration = 3000) => {
  ElMessage.success({
    message,
    duration,
    showClose: true
  })
}

// 错误消息
export const showError = (message: string, duration = 5000) => {
  ElMessage.error({
    message,
    duration,
    showClose: true
  })
}

// 警告消息
export const showWarning = (message: string, duration = 4000) => {
  ElMessage.warning({
    message,
    duration,
    showClose: true
  })
}

// 信息消息
export const showInfo = (message: string, duration = 3000) => {
  ElMessage.info({
    message,
    duration,
    showClose: true
  })
}

// 确认对话框
export const showConfirm = (
  message: string,
  title = '确认操作',
  options: {
    confirmButtonText?: string
    cancelButtonText?: string
    type?: 'warning' | 'info' | 'success' | 'error'
  } = {}
) => {
  return ElMessageBox.confirm(
    message,
    title,
    {
      confirmButtonText: options.confirmButtonText || '确定',
      cancelButtonText: options.cancelButtonText || '取消',
      type: options.type || 'warning',
      center: true
    }
  )
}

// 输入对话框
export const showPrompt = (
  message: string,
  title = '请输入',
  options: {
    inputPlaceholder?: string
    inputValue?: string
    inputValidator?: (value: string) => boolean | string
  } = {}
) => {
  return ElMessageBox.prompt(
    message,
    title,
    {
      inputPlaceholder: options.inputPlaceholder || '请输入内容',
      inputValue: options.inputValue || '',
      inputValidator: options.inputValidator,
      center: true
    }
  )
}

// 通知消息
export const showNotification = (
  title: string,
  message: string,
  type: 'success' | 'warning' | 'info' | 'error' = 'info',
  duration = 4500
) => {
  ElNotification({
    title,
    message,
    type,
    duration,
    position: 'top-right'
  })
}

// 操作成功通知
export const notifySuccess = (title: string, message: string) => {
  showNotification(title, message, 'success')
}

// 操作失败通知
export const notifyError = (title: string, message: string) => {
  showNotification(title, message, 'error', 6000)
}

// 操作警告通知
export const notifyWarning = (title: string, message: string) => {
  showNotification(title, message, 'warning')
}

// 处理API错误
export const handleApiError = (error: any, defaultMessage = '操作失败') => {
  let message = defaultMessage
  
  if (error?.response?.data?.error) {
    message = error.response.data.error
  } else if (error?.response?.data?.message) {
    message = error.response.data.message
  } else if (error?.message) {
    message = error.message
  }
  
  showError(message)
  return message
}

// 处理API成功
export const handleApiSuccess = (message: string, showNotify = false) => {
  if (showNotify) {
    notifySuccess('操作成功', message)
  } else {
    showSuccess(message)
  }
}

// 加载状态管理
export class LoadingManager {
  private loadingCount = 0
  private loadingInstance: any = null

  start(text = '加载中...') {
    this.loadingCount++
    if (this.loadingCount === 1) {
      this.loadingInstance = ElMessage({
        message: text,
        type: 'info',
        duration: 0,
        showClose: false
      })
    }
  }

  stop() {
    this.loadingCount = Math.max(0, this.loadingCount - 1)
    if (this.loadingCount === 0 && this.loadingInstance) {
      this.loadingInstance.close()
      this.loadingInstance = null
    }
  }

  clear() {
    this.loadingCount = 0
    if (this.loadingInstance) {
      this.loadingInstance.close()
      this.loadingInstance = null
    }
  }
}

// 全局加载管理器实例
export const globalLoading = new LoadingManager()
